<template>
	<view>
		<view>
			<view>
				<view class="header"><image src="/static/images/logo.png"></image></view>
				<view class="content">
				<!-- 	<view>申请获取以下权限</view>
					<text>获得你的公开信息(手机号、昵称、头像等)</text> -->
				</view>
				<button v-show="huoqu==0"  open-type="getUserInfo" @getuserinfo="getuserinfo">获取头像昵称</button>
				<button v-show="huoqu==1" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">点击获取手机号</button>
				
				<!-- <button  class="page_btn" hover-class="page_btn-hover" @tap="getPhoneOneClickUser">用户</button>
				<button  class="page_btn" hover-class="page_btn-hover" @tap="getPhoneOneClickOrg">机构</button> -->
			</view>
		</view>
	</view>
</template>
<script>
	import {
		HTTP_SERVER_URL,
		STATUS_CODE
	} from '../../config/index.js';
export default {
	data() {
		return {
			code:null,
			img:null,
			name:null,
			after:false,
			openId:null,
			sessionKey:null,
			encryptedData:null,
			iv:null,
			userData:null,
			huoqu:0
		};
	},
	onBackPress() {
		if (this.defaultButton != 'back') {
			return true;
		}
	},
	
	onLoad() {
		//获取用户code
		wx.login({
			provider:'weixin',
			success: (res) => {
			
				this.code=res.code
				console.log("code",this.code)
				uni.request({
					url:HTTP_SERVER_URL+'/aged/user/appletsLogin',
								method:"POST",
								data:{
									code:this.code
								},
								success:(res)=>{
									this.openId=res.data.data.openId
									this.sessionKey=res.data.data.sessionKey
									console.log('openid',this.openId)
								},
								fail:(res)=>{
									console.log('失败失败',res)
								}
				})
				
			}
		})
	},
	methods: {
		getuserinfo:function(e){
			console.log(e.detail.userInfo.avatarUrl)
			console.log(e.detail.userInfo.nickName)
			this.huoqu=1
			this.img=e.detail.userInfo.avatarUrl
			this.name=e.detail.userInfo.nickName
		},
		
		getPhoneNumber:function(e){
			//根据openid等获取用户信息
			
			
		        if(e.detail.errMsg=="getPhoneNumber:fail user deny"){
		        }else{       
					this.encryptedData =e.detail.encryptedData 
					this.iv=e.detail.iv
		            e.detail.encryptedData      //加密的用户信息  
		            e.detail.iv     //加密算法的初始向量  时要用到
					console.log('加密内容',e.detail.encryptedData,'用户iv',e.detail.iv)
					console.log('keyyyy的',this.sessionKey)
		        } 
				
				 uni.request({
				 	url:HTTP_SERVER_URL+'/aged/user/login',
				 				method:"POST",
				 				data:{
				 					openId:this.openId,
									sessionKey:this.sessionKey,
									encryptedData:this.encryptedData,
									iv:this.iv,
									wxAvatar: this.img,
									wxNickname:this.name
				 				},
				 				success:(res)=>{
				 					console.log('登录',res)
									uni.setStorageSync('phone',res.data.data.phone)
									if(res.data.data.isUser==0){
										uni.showToast({
											title:'您还未注册，请先完成注册再进行以下操作',
											position:"center",
											icon:"none"
										})
										setTimeout(()=>{
											uni.navigateTo({
												url:'rigest'
											})
										},2000)
										
									}else if(res.data.data.isUser==3){
										uni.showToast({
											title:'您的信息正在审核中，请稍候...',
											position:"center",
											icon:"none"
										})
									}else if(res.data.data.isUser==4){
										uni.showToast({
											title:'你的审核未通过，请完善信息后再次提交审核',
											position:"center",
											icon:"none"
										})
									}else{
										let userData=res.data.data
										userData.wxAvatar=this.img
										userData.wxNickname=this.name
										uni.clearStorageSync('open')
										uni.setStorageSync('open',userData)
										console.log('触发登录事件设定缓存，',userData)
										
										this.$store.dispatch('wechatLogin', userData).then(res => {
											setTimeout(() => {
												uni.switchTab({
													url: '/pages/index/index'
												});
											}, 1100);
										});
										
									}
									
				 				},
				 })
				
		    } 
		,
		// onGetPhoneNumber(e){  
		//         if(e.detail.errMsg=="getPhoneNumber:fail user deny"){       //用户决绝授权  
		//             //拒绝授权后弹出一些提示  
		// 			console.log('拒绝')
		//         }else{      //允许授权  
		//             console.log(e.detail.encryptedData)
		// 			this.encryptedData =e.detail.encryptedData 
		// 			this.iv=e.detail.iv
		//             e.detail.encryptedData      //加密的用户信息  
		//             e.detail.iv     //加密算法的初始向量  时要用到  
		// 			console.log(e.detail.iv)
		//         } 
		// 		 // uni.request({
		// 		 // 	url:HTTP_SERVER_URL+'/aged/user/login',
		// 		 // 				method:"POST",
		// 		 // 				data:{
		// 		 // 					openId:this.openId,
		// 			// 				sessionKey:this.sessionKey,
		// 			// 				encryptedData:e.detail.encryptedData,
		// 			// 				iv:e.detail.iv
		// 		 // 				},
		// 		 // 				success:(res)=>{
		// 		 // 					console.log('登录',res)
				 					
		// 		 // 				}
		// 		 // })
		//     } ,
			  
		getPhoneOneClickUser() {
			
			let userData = {
				age: null,
				id: 3,
				instituteId: null,
				isUser: 1,
				juzhudi: '大曌',
				jwt: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJ7XCJ1c2VySWRcIjozLFwidXNlclR5cGVcIjowfSJ9.h8FMVW7sVEYQYW-XKi01i7EMQ1Ea0qwtiJFnqc1Hub8',
				openId: 'sf12df54sd56fsd32fds2f1',
				owner: null,
				phone: '18433666654',
				sex: 1,
				shequ: null,
				userType: 0,
				username: 'yao用户',
				wxAvatar: 'https://wallpaperm.cmcm.com/2d6a574669e918214836371511fea5b8.jpg',
				wxNickname: '朗逸'
			};
			this.$store.dispatch('wechatLogin', this.userData).then(res => {
				setTimeout(() => {
					uni.switchTab({
						url: '/pages/index/index'
					});
				}, 1100);
			});
		},
		getPhoneOneClickOrg() {
			let orgData = {
				age: null,
				id: 6,
				instituteId: null,
				isUser: 1,
				juzhudi: null,
				jwt: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJ7XCJ1c2VySWRcIjo2LFwidXNlclR5cGVcIjoxfSJ9.ILaqq32o5ZVeFxE5lN7Qs9qLV1yjflbH1PxsLdShmSQ',
				openId: 'sf12df54sd56fsd32fds2f1',
				owner: 1,
				phone: '18813022201',
				sex: null,
				shequ: null,
				userType: 1,
				username: null,
				wxAvatar: 'https://wallpaperm.cmcm.com/2d6a574669e918214836371511fea5b8.jpg',
				wxNickname: '朗逸'
			};
			this.$store.dispatch('wechatLogin', orgData).then(res => {
				setTimeout(() => {
					uni.switchTab({
						url: '/pages/index/index'
					});
				}, 1100);
			});
		}
	}
};
</script>

<style lang="stylus">
.header
	margin 110rpx 0 90rpx 50rpx
	border-bottom 1px solid #ccc
	text-align center
	width 650rpx
	height 300rpx
	line-height 450rpx
.header image
	width 200rpx
	height 200rpx
.content
	margin-left 50rpx
	margin-bottom 90rpx
.content text
	display block
	color #9d9d9d
	margin-top 40rpx
.page_btn-hover
	opacity 0.5
.page_btn
	flex 1
	margin 70rpx 50rpx
	height 90rpx
	display flex
	align-items center
	justify-content center
	background #2879fd
	border-radius 80rpx
	font-size 32rpx
	font-family PingFang SC
	font-weight 400
	color #FFFFFF
	border none
	outline none
	&:nth-of-type(2)
		margin-left 45rpx
</style>
